package xuanke;
public class xuankexitong {
 // Course 类：新增 teacherName 属性存储授课教师姓名
	    static class Course {
	        private String id;
	        private String name;
	        private String location;
	        private String time;
	        private String teacherName; // 存储该课程的授课教师姓名

	        // 构造方法新增 teacherName 参数
	        public Course(String id, String name, String location, String time, String teacherName) {
	            this.id = id;
	            this.name = name;
	            this.location = location;
	            this.time = time;
	            this.teacherName = teacherName;
	        }

	        // getter 方法：获取授课教师姓名
	        public String getTeacherName() {
	            return teacherName;
	        }

	        public String getId() {
	            return id;
	        }

	        public String getName() {
	            return name;
	        }

	        public String getLocation() {
	            return location;
	        }

	        public String getTime() {
	            return time;
	        }

	        // 重写 toString：课表中显示授课教师姓名
	        @Override
	        public String toString() {
	            return "课程编号：" + id + "，课程名称：" + name + "，授课教师：" + teacherName 
	                   + "，上课地点：" + location + "，上课时间：" + time;
	        }
	    }

	    // Teacher 类：创建课程时传入教师姓名，绑定课程与教师
	    static class Teacher {
	        private String id;
	        private String name;
	        private String gender;
	        private java.util.List<Course> taughtCourses = new java.util.ArrayList<>();

	        public Teacher(String id, String name, String gender) {
	            this.id = id;
	            this.name = name;
	            this.gender = gender;
	        }

	        // 开设课程时，将当前教师姓名传入 Course 构造方法
	        public Course offerCourse(String courseId, String courseName, String location, String time) {
	            Course course = new Course(courseId, courseName, location, time, this.name);
	            taughtCourses.add(course);
	            System.out.println(name + "（" + gender + "）老师开设了" + courseName + "课程");
	            return course; // 返回创建的课程，供学生选择
	        }

	        //  getter 方法
	        public String getId() {
	            return id;
	        }

	        public String getName() {
	            return name;
	        }

	        public String getGender() {
	            return gender;
	        }

	        public java.util.List<Course> getTaughtCourses() {
	            return taughtCourses;
	        }
	    }

	    // Student 类：逻辑不变，课表会自动显示 Course 中绑定的教师姓名
	    static class Student {
	        private String id;
	        private String name;
	        private String gender;
	        private java.util.List<Course> selectedCourses = new java.util.ArrayList<>();

	        public Student(String id, String name, String gender) {
	            this.id = id;
	            this.name = name;
	            this.gender = gender;
	        }

	        public void selectCourse(Course course) {
	            selectedCourses.add(course);
	            System.out.println(name + "（" + gender + "）同学选了" + course.getName() + "课程（授课教师：" + course.getTeacherName() + "）");
	        }

	        public void dropCourse(Course course) {
	            if (selectedCourses.remove(course)) {
	                System.out.println(name + "（" + gender + "）同学退了" + course.getName() + "课程");
	            } else {
	                System.out.println(name + "（" + gender + "）同学未选" + course.getName() + "课程，无法退课");
	            }
	        }

	        public void printCourseSchedule() {
	            System.out.println("\n" + name + "（" + gender + "）同学的课表：");
	            for (Course course : selectedCourses) {
	                System.out.println(course); // 直接调用 Course 的 toString，包含教师姓名
	            }
	        }

	        // getter 方法
	        public String getId() {
	            return id;
	        }

	        public String getName() {
	            return name;
	        }

	        public String getGender() {
	            return gender;
	        }

	        public java.util.List<Course> getSelectedCourses() {
	            return selectedCourses;
	        }
	    }

	    // 主方法：测试逻辑
	    public static void main(String[] args) {
	        // 1. 创建教师
	        Teacher teacher1 = new Teacher("T001", "张老师", "男");
	        Teacher teacher2 = new Teacher("T002", "李老师", "女");

	        // 2. 教师开设课程（自动绑定教师姓名到课程）
	        Course course1 = teacher1.offerCourse("C001", "Java编程", "A栋101", "周一上午");
	        Course course2 = teacher2.offerCourse("C002", "数据结构", "B栋202", "周二下午");

	        // 3. 创建学生并选课、退课、打印课表
	        Student student1 = new Student("S001", "小明", "男");
	        student1.selectCourse(course1);
	        student1.selectCourse(course2);
	        student1.dropCourse(course1);
	        student1.printCourseSchedule();

	        Student student2 = new Student("S002", "小红", "女");
	        student2.selectCourse(course2);
	        student2.printCourseSchedule();
	    }
	}